<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>

<script>
function order(obj) {
	return fncOrderNoMember('2',obj);   //비회원
}

///////////////////////////////////////////////////////////////////////////////
//  비회원 장바구니 선택삭제                                                 //
/////////////////////////////////////////////////////////////////////////////// 
function deleteCartAll(prdSap) {
	var cartList = $.cookies.get('cartList');
	if(cartList == null){
     	alert("삭제할 상품이 없습니다.");
   	}
	for(var i=0;i<prdSap.length;i++){
	  for(var j=0;j<cartList.length;j++){
        if(prdSap[i]==cartList[j].a){
        cartList.splice(j,1);      
        $.cookies.set('cartList', cartList);  
       }
	  }
	}		
	location.href="/vhmall/cart_guest.vh";
}
	
///////////////////////////////////////////////////////////////////////////////
//선택된 상품만 (삭제,찜)                                                  //
///////////////////////////////////////////////////////////////////////////////
function chkPrd(mode){
	var checkPrd = new Array();
	$('input:checkbox[name=chkSign]:checked').each(function(i){       
	  checkPrd[i] = $(this).get(0).value;
	});
	 if(checkPrd<=0){
	   alert("상품을 선택해주세요");
	   return false;
	 }
	deleteCartAll(checkPrd);	
}

function fncOrderNoMember(flag,obj){
    var chkSign = $("input:checkbox[name=chkSign]");
    if( chkSign.length <= 0 ){
      alert("구매하실 상품을 선택하세요.");
      return false;
    }
  	//상품없이 주문할때
    var prdSap = new Array();
    $('input:checkbox[name=chkSign]:checked').each(function(i){       
       prdSap[i] = $(this).get(0).value;
    });      
    //상품없이 주문할때
    if(prdSap.length<=0 && obj !="A"){
      alert("구매하실 상품을 선택하세요.");
      return false;
    }    
    var chkQty = false;
    $(".in input").each(function(){
      if(Number($(this).val()) <= 0 ){
        alert("수량이 0인 상품이 있습니다.\n주문하실 상품의 수량을 선택해 주세요.");
        chkQty = true;
      }
    });
    if(chkQty) return false;
    //장바구니
     $.cookies.set('orderList','[]', { path:'/'});  //초기화       
     var orderList = $.cookies.get('orderList');
     var cartList = $.cookies.get('cartList');

    //체크선택
     for(var i=0;i<prdSap.length;i++){
       for(var j=0;j<cartList.length;j++){  
          if(prdSap[i]==cartList[j].a){            
          	orderList.push( {a: cartList[j].a, b: Number(cartList[j].b), c: Number(cartList[j].c), d: cartList[j].d} );             
         }
       }
     } 
     $.cookies.set('orderList', orderList ); 
      
    location.href="/vhmall/order_guest.vh";
  }
</script>

<script>
function count_checkobx() {
	var cnt = 0;
	$("input[name='chkSign']:checkbox").each(function(){ 
    	if($(this).is(":checked")) {
    		cnt++;
    	}
    }); 
    
    $("#cart_cnt").html(cnt);
}

$(document).ready(function() {
	$("#all_checkbox").click(function() {
		var check = $("#all_checkbox").is(":checked"); 
	    $("input[name='chkSign']:checkbox").each(function(){ 
	    	$(this).prop("checked", check);
	    }); 
	    
	    count_checkobx();
	});
	
	$("input[name='chkSign']:checkbox").click(function() {
	    count_checkobx();
	});
});
</script>

<!-- 콘텐츠 영역 -->
    <div id="BodyWrap">
        <section>
            <h4 class="pag_title" title="장바구니">장바구니</h4>
            <div id="ContentWrap4">
            	<div class="mar_b10">
                	<div class="inp_ckb inp_ckb3">
                        <input type="checkbox" class="ckb" name="all_checkbox" id="all_checkbox">
                        <label for="all_checkbox">주문상품 총 <span class="ftc_red" id="cart_cnt">0</span>개</label>
                    </div>
                </div>
                <form name="cartForm" method="post">
                <ul class="showwindow_list3">
                	<c:forEach items="${requestScope.itemList}" var="items" varStatus="status">
	                	<li>
	                    	<div class="inp_ckwrap2">
	                        	<div class="inp_ckb2">
	                            	<input type="checkbox" class="ckb" name="chkSign" id="chkSign${status.index}" value="${items.PRDCD}_${items.SAPCD}">
	                                <label for="all_checkbox">&nbsp;</label>
	                            </div>
	                        </div>
	                    	<a href="/vhmall/item_detail.vh?prdcd=${items.PRDCD}" class="thumb">
	                            <img src="http://www.vhmall.co.kr/inc/imgLoad.jsp?dirName=product/${items.CORPNO}/&fileName=${items.PRDIMG1_ENC}&thumb=Y&quality=100&size=230" />
	                        </a>
	                        <div class="windowinfo_area">
	                            <a href="/vhmall/item_detail.vh?prdcd=${items.PRDCD}" class="title">${items.PRDNM }</a>
	                            <p class="price">
	                                ${items.SELPRC}원
	                            </p>
	                            <p>
	                            	${items.PRDNONM} &nbsp;&nbsp;&nbsp;${items.BUYQTY}개
	                            </p>
	                            <p class="btnposition_box">
	                                <a href="#" class="btn_inlistyle1 gradation_gray1" title="삭제하기" onclick="deleteCart('${items.PRDCD}_${items.SAPCD}');">삭제하기</a>
	                                <a href="javascript:alert('준비중입니다.');" class="btn_inlistyle1 gradation_gray1 ftc_darkred" title="1:1문의">1:1문의</a>
	                            </p>
	                        </div>
	                    </li>
                    </c:forEach>
                </ul>
                </form>
                <div class="btnposition_itembuy">
                    <div class="btnwline3">
                        <a href="javascript:alert('로그인 후 이용가능한 메뉴입니다.');" class="heart" title="찜하기">찜하기</a>
                        <a href="javascript:chkPrd('delSel');" class="delete" title="삭제하기">삭제하기</a>
                        <a href="javascript:order();" title="주문하기">주문하기</a>
                    </div>
                </div>
            </div>
        </section>
        <!--// 콘텐츠 영역 -->